//
//  RankCell.m
//  体育
//
//  Created by  on 2022/10/24.
//

#import "RankCell.h"
#import "RankItem.h"
@interface RankCell()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewH;
@property (strong, nonatomic)  NSArray *titles;
@property (strong, nonatomic)  NSMutableArray *totalArray;
@property (strong, nonatomic)  NSMutableArray *homeArray;
@property (strong, nonatomic)  NSMutableArray *fromeArray;
@property (strong, nonatomic)  NSMutableArray *leastArray;

@end
@implementation RankCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.collectionView registerNib:[RankItem nib] forCellWithReuseIdentifier:[RankItem ID]];
    self.collectionViewH.constant = 5 * 26;
}

- (void)setHomename:(NSString *)homename{
    _homename =  homename;
    self.name.text = homename;
    self.titles = @[@"全场",@"赛",@"胜",@"平",@"负",@"得",@"失",@"积分",@"排名",@"胜率"];
    self.totalArray = [NSMutableArray arrayWithArray:@[@"总"]];
    self.homeArray = [NSMutableArray arrayWithArray:@[@"主"]];
    self.fromeArray = [NSMutableArray arrayWithArray:@[@"客"]];
    self.leastArray = [NSMutableArray arrayWithArray:@[@"近",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0%"]];
    int rank = (arc4random() % 30) + 1;
    [DataTool findName:homename complete:^(NSMutableArray<Record *> * _Nonnull result) {
        [self setupArray:self.totalArray result:result rank:rank];
    }];
    [DataTool findHomeName:homename complete:^(NSMutableArray<Record *> * _Nonnull result) {
        [self setupArray:self.homeArray result:result rank:rank];
    }];
    [DataTool findFromName:homename complete:^(NSMutableArray<Record *> * _Nonnull result) {
        [self setupArray:self.fromeArray result:result rank:rank];
    }];
}
- (void)setupArray:(NSMutableArray *)array result:(NSArray *)result rank:(int)rank{
    int count = (int)result.count;
    int win = 0;
    int equal = 0;
    int lose = 0;
    int score = 0;
    int score_lose = 0;
    int jifen = 0;
    for (Record *model in result) {
        if (model.score_from == model.score_home) {
            equal += 1;
            score += model.score_home;
            score_lose += model.score_from;
        }
        if ([model.home isEqualToString:self.homename] && model.win) {
            win += 1;
            score += model.score_home;
            score_lose += model.score_from;
        }else if ([model.from isEqualToString:self.homename] && model.score_from > model.score_home){
            win += 1;
            score += model.score_home;
            score_lose += model.score_from;
        }
    }
    jifen = win * 3 + equal;
    lose = count - win - equal;
    [array addObject:[NSString stringWithFormat:@"%d",count]];
    [array addObject:[NSString stringWithFormat:@"%d",win]];
    [array addObject:[NSString stringWithFormat:@"%d",equal]];
    [array addObject:[NSString stringWithFormat:@"%d",lose]];
    [array addObject:[NSString stringWithFormat:@"%d",score]];
    [array addObject:[NSString stringWithFormat:@"%d",score_lose]];
    [array addObject:[NSString stringWithFormat:@"%d",jifen]];
    [array addObject:[NSString stringWithFormat:@"%d",rank]];
    [array addObject:[NSString stringWithFormat:@"%d%%",(int)(win * 100.0 / count)]];
    [self.collectionView reloadData];

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 5;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RankItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RankItem ID] forIndexPath:indexPath];
    if (indexPath.section == 0 && self.titles.count > indexPath.row) {
        cell.content.text = LS(self.titles[indexPath.row]);
    }else if (indexPath.section == 1 && self.totalArray.count > indexPath.row){
        cell.content.text = LS(self.totalArray[indexPath.row]);
    }else if (indexPath.section == 2 && self.homeArray.count > indexPath.row){
        cell.content.text = LS(self.homeArray[indexPath.row]);
    }else if (indexPath.section == 3 && self.fromeArray.count > indexPath.row){
        cell.content.text = LS(self.fromeArray[indexPath.row]);
    }else if (indexPath.section == 4 && self.leastArray.count > indexPath.row){
        cell.content.text = LS(self.leastArray[indexPath.row]);
    }
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake((SCREEN_WIDTH - 16 - 16) / 10.0,26);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 8, 0, 8);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end
